package SwordForOffer.Problem43;

public class DicesProbability {
	/*
	 * 把n个骰子仍在地上，所有骰子朝上一面的点数之和为s,输入n,打印出s的所有可能出现的概率
	 */
	public void printProbability(int number) {
		if(number<1)
			return ;

		int g_maxValue=6;

		int[][] probabilities=new int[2][];

		probabilities[0]=new int[g_maxValue*number+1];
		probabilities[1]=new int[g_maxValue*number+1];

		int flag=0;
		for(int i=1;i<=g_maxValue;i++)
			probabilities[0][i]=1;

		for(int k=2;k<=number;++k) {
			for(int i=0;i<k;++i)
				probabilities[1-flag][i]=0;

			for(int i=k;i<=g_maxValue*k;++i) {
				probabilities[1-flag][i]=0;
				for(int j=1;j<=i && j<=g_maxValue;++j)
					probabilities[1-flag][i]+=probabilities[flag][i-j];
			}
			flag=1-flag;
		}

		double total=Math.pow(g_maxValue, number);
		for(int i=number;i<=g_maxValue*number;i++) {
			double ratio=(double) probabilities[flag][i]/total;
			System.out.println(i);
			System.out.println(ratio);
		}
	}

	public void myPrintProbability(int n) {
		if (n < 1){
			return;
		}
		int maxValue = 6;
		int[] time = new int[maxValue*n + 1];
		int[] timeTemp = new int[maxValue*n + 1];

		for (int i = 1;i <= 6; i ++){
			time[i] = 1;
		}
		for (int i = 1;i < n;i++){
			for (int j = i + 1;j <= maxValue*(i + 1); j++){
				for (int k = 1;k <= maxValue;k++){
					if (j - k > 0){
						timeTemp[j] += time[j - k];
					}else {
						break;
					}
				}
			}
			time = timeTemp;
			timeTemp = new int[maxValue*n + 1];
		}
		double totalConditions = Math.pow(maxValue,n);
		for (int i = n;i < time.length ;i ++){
			System.out.println(i);
			System.out.println("paobalities = " + time[i] / totalConditions);
		}
	}
}
